package action.system;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import services.system.UploadServices;
import com.util.Component;
import com.Const;
import com.PublicSystem;
/**
* PersonMgrFacade:文件下载
* @author bjh
* @version Fri May 06 17:59:17 CST 2016 gjx
* @since 1.0
*/
public class FileUploadAction extends HttpServlet {	
	PublicSystem sys = PublicSystem.getInstance();
	UploadServices ss = new UploadServices();
	private static final long serialVersionUID = 1L;
	/**
	 * 获取请求
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	/**
	 *  文件下载
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			boolean flag = false;
			DiskFileItemFactory factory = new DiskFileItemFactory();
			HashMap<String, String> map = new HashMap<String, String>();
			// 设置内存缓冲区，超过后写入临时文件
			factory.setSizeThreshold(10240000);
			// 设置临时文件存储位置
			String base = Component.getQuest("urlpath", request);
			String czsj = Component.systime_all();
			String times = czsj.substring(0,4);
			String fileurlpath = Const.server_path + base;
			String userid = Component.getSession("userid", request);
			String planid = Component.getQuest("planid", request);
			File file = new File(fileurlpath);
			if(!file.exists())
			file.mkdirs();
			factory.setRepository(file);
			ServletFileUpload upload = new ServletFileUpload(factory);
			// 设置单个文件的最大上传值
			upload.setFileSizeMax(102400000l);
			// 设置整个request的最大值
			upload.setSizeMax(102400000l);
			upload.setHeaderEncoding("UTF-8");
			List<?> items = upload.parseRequest(request);
			FileItem item = null;
			String fileName = null;
			for (int i = 0 ;i < items.size(); i++){
				item = (FileItem) items.get(i);
				fileName = item.getName();
				// 保存文件
				if (!item.isFormField() && item.getName().length() > 0) {
					// 五位随机数
					int radNum = (int)(Math.random() * 100000);
					String ffType = fileName.substring(fileName.lastIndexOf(".") + 1);
					for(int j = 0; j < Const.dataType.length; j++) {
						if(Const.dataType[j].trim().equals(ffType)) {
							flag = true;
						}
					}
					String encodeFileName = times + "_" + radNum + "." + ffType;
					String fileurl = base + "/" + encodeFileName;
					if(true == flag) {
						item.write(new File(fileurlpath + "/" + encodeFileName));
						map.put("fileurl", fileurl);
						map.put("filename", fileName);
						map.put("fileseq", String.valueOf(i));
						map.put("czr", userid);
						map.put("czsj", czsj);
						map.put("jhid", planid);
						ss.insertFile(map);
					} else {
						throw new IOException("IP地址" + request.getRemoteAddr() + "上传的附件后缀名为" + ffType + "的类型不符合要求");  
					}
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
